namespace DCHealthCheck.DomainModel {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Diagnostics;
    using System.Text;   
    #endregion

    [Table("TagObjectValues")]
    public class TagObjectValue : ITagObjectValue, IObjectWithState, IValidatableObject {

        #region Members
        public virtual long Id { get; set; }
        public virtual DateTime TagValue { get; set; }

        #region Related Entities
        public virtual SystemInfo SystemInfo {
            get { return systemInfo; }
            set {
                systemInfo = value;
                if (systemInfo != null) {
                    if (this.SystemInfoId != systemInfo.Id) {
                        this.SystemInfoId = systemInfo.Id;
                    }
                }
            }
        }
        private SystemInfo systemInfo;

        [ForeignKey("SystemInfo")]
        public virtual Guid SystemInfoId {
            get {
                if (this.SystemInfo != null) return this.SystemInfo.Id;
                return systemInfoId;
            }
            set { systemInfoId = value; }
        }
        private Guid systemInfoId;

        public virtual TagObject TagObject {
            get { return tagObject; }
            set {
                tagObject = value;
                if (tagObject != null) {
                    if ((this.TagObjectId == null) || (this.TagObjectId != tagObject.DN)) {
                        this.TagObjectId = tagObject.DN;
                    }
                }
                else {
                    if (this.TagObjectId != null) {
                        this.TagObjectId = null;
                    }
                }
            }
        }
        private TagObject tagObject;

        //[ForeignKey("TagObject")]
        public virtual string TagObjectId {
            get {
                if (this.TagObject != null) return this.TagObject.DN;
                return tagObjectId;
            }
            set { tagObjectId = value; }
        }
        private string tagObjectId;
        #endregion

        #region NotMapped properties
        [NotMapped]
        public DetachedState DetachedState { get; set; }

        [NotMapped]
        public Object EntityKey { get { return this.Id; } }

        [NotMapped]
        public Dictionary<string, object> StartingOriginalValues { get; set; }
        #endregion 
        #endregion

        #region Constructor
        public TagObjectValue() {
        } 
        #endregion

        #region Methods
        [DebuggerStepThroughAttribute]
        public override string ToString() {
            var info = new StringBuilder();

            info.Append("TagObjectValue: ");
            if ((this.SystemInfo != null) && (this.SystemInfo.DomainController != null) && (this.SystemInfo.DomainController.DNSName != null)) {
                info.AppendFormat("DC: {0}; ", this.SystemInfo.DomainController.DNSName);
            }
            if (this.SystemInfo != null) {
                info.AppendFormat("HealthCheck Date: {0}; ", this.SystemInfo.CheckDateTimeUTC.ToString("yyyy-MMM-dd"));
            }
            if ((this.TagObject != null) && (this.TagObject.DN != null)) {
                info.AppendFormat("Tag Object DN: {0}; ", this.TagObject.DN);
            }
            info.AppendFormat("Tag Object Value: {0}", this.TagValue.ToString("yyyy-MMM-dd HH:mm"));

            return info.ToString();
        }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
            if ((this.DetachedState != DetachedState.Added) && (this.Id < 1)) {
                yield return new ValidationResult(
                    "Id is required.", new[] { "Id" });
            }

            if (this.DetachedState != DetachedState.Deleted) {
                // add entity to SystemInfo, let DetectChanges perform relationship fixup
                if (this.SystemInfoId == Guid.Empty) {
                    yield return new ValidationResult(
                        "SystemInfoId is required.", new[] { "SystemInfoId" });
                }

                if (this.TagObjectId == null) {
                    if ((this.TagObject == null) || (this.TagObject.DetachedState != DomainModel.DetachedState.Added)) {
                        yield return new ValidationResult(
                            "TagObjectId is required.", new[] { "TagObjectId" });
                    }
                }

                if ((this.SystemInfo != null) && (this.SystemInfo.Id != this.SystemInfoId)) {
                    yield return new ValidationResult(
                        "SystemInfo.Id does not match SystemInfoId.", new[] { "SystemInfo, SystemInfoId" });
                }

                if (((this.TagObject != null) && (this.TagObject.DN != null)) && ((this.TagObjectId == null) || (this.TagObject.DN != this.TagObjectId))) {
                    yield return new ValidationResult(
                        "TagObject.DN does not match TagObjectId.", new[] { "TagObject, TagObjectId" });
                }

                if (this.TagValue == DateTime.MinValue) {
                    yield return new ValidationResult(
                        string.Format("TagValue invalid: {0}.  Must be greater than: {1}",
                        this.TagValue.ToString("yyyy-mm-dd HH:mm"), DateTime.MinValue.ToString("yyyy-mm-dd HH:mm")),
                        new[] { "TagValue" });
                }
            } // if (this.DetachedState != State.Deleted)
        }  
        #endregion
        
    }
}
